package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberClientManageBinding;
import com.sgb.member.di.DaggerMemberComponent;
import com.sgb.member.di.MemberModule;
import com.sgb.member.entity.MemberClientListBean;
import com.sgb.member.ui.adapter.MemberClientAdapter;
import com.sgb.member.view_model.ClientManageModel;
import com.sgb.member.weight.RoundCornerDialog;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * Time:2021/8/26
 * AUTHOR:F_Q
 * Description:用户中心-会员-客户管理
 */
public class MemberClientManageActivity extends AppActivity {

    @Inject
    ClientManageModel modelView;
    private ActivityMemberClientManageBinding mMemberClient;

    private int ADDCLIENT = 9;
    private MemberClientAdapter memberclientadapter;
    private boolean isLoadMore = false;

    public static void start(Context context) {
        Intent intent = new Intent(context, MemberClientManageActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMemberComponent.builder()
                .appComponent(appComponent)
                .memberModule(new MemberModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        mMemberClient = DataBindingUtil.setContentView(this, R.layout.activity_member_client_manage);
        mMemberClient.title.titleView.setText("客户管理");
        mMemberClient.title.rightView.setText("新增");
        mMemberClient.title.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mMemberClient.title.rightView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //新增客户管理界面
                startActivityForResult(new Intent(getApplicationContext(),MemberAddClientActivity.class),ADDCLIENT);
            }
        });
        initRecyclerView();
    }

    private void initRecyclerView() {
        if (memberclientadapter == null) {
            memberclientadapter = new MemberClientAdapter(this, null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mMemberClient.recyclerView.setLayoutManager(layoutManager);
        mMemberClient.recyclerView.setHasFixedSize(true);   
        //解决数据加载完成后, 没有停留在顶部的问题
        mMemberClient.recyclerView.setFocusable(false);
        mMemberClient.recyclerView.setAdapter(memberclientadapter);
        mMemberClient.recyclerView.setLoadingMoreEnabled(true);
        mMemberClient.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getData();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                //上拉加载更多
                modelView.getMemberClientList(isLoadMore);
            }
        });
        memberclientadapter.setItemOnClick(new MemberClientAdapter.ItemOnClick() {
            @Override
            public void Edit(int pos) {
                //编辑
                startActivityForResult(new Intent(getApplicationContext(),MemberAddClientActivity.class).putExtra("data",memberclientadapter.getDatas().get(pos)),ADDCLIENT);
            }

            @Override
            public void Delete(String no) {
                showDialog(no);
            }

            @Override
            public void CompanyWeb(String companyNo) {
                //跳转到企业详情
                MemberConfig.getInstance().startWebsiteInfo(getActivity(),companyNo,1);
            }
        });
    }

    private void showDialog(String no) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失
        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("确认删除当前客户?");

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
                //删除
                modelView.getMemberClientDelete(no);
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }

    @Override
    protected void init() {
        super.init();
       getData();
    }

    private void getData() {
        isLoadMore = false;
        modelView.getMemberClientList(isLoadMore);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode ==ADDCLIENT){
                //更新数据信息
                getData();
            }
        }
    }

    /**
     * 列表返回接口
     * @param data
     * @param isLastPage
     */
    public void setClientList(List<MemberClientListBean> data, boolean isLastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                memberclientadapter.addDatas(data);
            }
            mMemberClient.recyclerView.setNoMore(isLastPage);
        } else {
            if (data != null && data.size() > 0) {
                mMemberClient.emptyView.setVisibility(View.GONE);
                mMemberClient.recyclerView.setVisibility(View.VISIBLE);
                memberclientadapter.setDatas(data);
                mMemberClient.recyclerView.scrollToPosition(0);
                mMemberClient.recyclerView.setNoMore(isLastPage);
            } else {
                memberclientadapter.clearDatas();
                mMemberClient.emptyView.setVisibility(View.VISIBLE);
                mMemberClient.recyclerView.setVisibility(View.GONE);
            }
        }
    }

    private void RecyclerRefreshing() {
        mMemberClient.recyclerView.refreshComplete();
        mMemberClient.recyclerView.loadMoreComplete();
    }

    /**
     * 删除信息成功更新列表信息
     */
    public void setMemberClientDelete() {
        //更新数据信息
        getData();
    }
}
